const Router = require('koa-router');
const question= new Router();
const sqldata = require('./sqldata');

question.get('/', async (ctx)=>{
    let mydata = await new Promise((resolve, reject)=>{
        let sql = `select * from question`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
    ctx.body = mydata;
})
question.post('/',async(ctx)=>{
    let val=ctx.request.body;
    let sql;
    let mydata;
    mydata = await new Promise((resolve, reject)=>{
        sql = `insert into question (Qtype,Qinfor,Qanswer,Qoption1,Qoption2,Qoption3,Qoption4) values ('${val.Qtype}','${val.Qinfor}','${val.Qanswer}','${val.Qoption1}','${val.Qoption2}','${val.Qoption3}','${val.Qoption4}')`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
})
question.delete('/',async(ctx)=>{
    let val=JSON.parse(ctx.request.body);
    let sql;
    let mydata;
    mydata = await new Promise((resolve, reject)=>{
        sql = `delete from question where Qid ='${val.Qid}'`;
        return sqldata(sql, (err, data)=>{
            if(err) reject(err);
            resolve(data);	
        })
    })
})

module.exports = question;